$(function(){
	//选中	、  展开有子节点的菜单  、跳转
	$("body").on("click","div.menu_node",function(event){  
		 selectMenu($(this));
	 }); 
 
 	$.ajax({
		   url:URL.FRAMEWORK+"/menu/find",
		   success:function(response){ 
			 createMenu(response.data);
		   }
	})
});



function createMenu(menuJson){ 
	    var  menuHtml  = "<ul class='menu'>";
		for(var i = 0 ; i< menuJson.length;i++){
	 			var menu = menuJson[i];
	 			menuHtml+="<li >";
	 			var flag = (menu.children && menu.children.length >0);
	 		    //判断是否是父节点 添加parent样式
	 			if(flag){
	 				menuHtml+="<div class='parent menu_node'>";
	 			}else{
	 				menuHtml+="<div  class='menu_node'>";
	 			}
	 			//判断是否有图标
	 			if(menu.attribute.icon){
	 				menuHtml+="<span style=\"background-image: url('"+menu.attribute.icon+"');background-repeat:no-repeat; width: 24px;height: 30px;float: left;margin-top: 10px;\"> </span>";
	 			}
	 			//判断是否是连接
	 			if(menu.attribute.linked == 3){
	 				var css = menu.attribute.css;
	 				menuHtml+="<span class='menu_link "+(css?css:'')+"' link='"+menu.id+"'>"+menu.name+"</span>";
	 			}else{
	 				menuHtml+="<span>"+menu.name+"</span>";
	 			}
	 		    //判断是否是父节点 添加层叠图标
	 			if(flag){
	 				menuHtml+="<i></i>";
	 			}
	 			menuHtml+="</div>";
	 			//判断是否是父节点 添加子节点
	 			if(flag){
	 				menuHtml  += "<ul >"
	 				createMenu(menu.children); 
	 				menuHtml  += "</ul >"
	 			}else{
	 				menuHtml+="</li>"; 
	 			}  
		  }
		menuHtml+="</li>";
		menuHtml += "</ul>";
		$("body").append(menuHtml);
		$("ul.menu .homepage").click();
} 

function selectMenu(that){
	  //选中
	  $("li>div.select").css("background-color","").removeClass("select");
	  that.toggleClass("select");  

	  //展开有子节点的菜单
	  if(that.hasClass("parent")){
		  that.siblings("ul").toggleClass("show"); 
		  that.find("i").toggleClass("downshow"); 
	  }
	  //跳转
	  var menuLik = that.find("span.menu_link");
	  var newSelectTab = null;
	  if( menuLik.length > 0){ 
		  var menuId = menuLik.attr("link"); 
		  newSelectTab =  openTabByMenuId(menuId);
	   } 
	   changeColor(that,newSelectTab); 
}

function openTabByMenuId(menuId){
	var selectMenu = $(".menu .select");
    var tabId = "main_tab_"+menuId;
    var iframePrefix = "iframe_";
	var iframeId = iframePrefix+ tabId;
    var mainWindow = $(top.$("iframe.rigth_iframe")[0].contentWindow.document);
    var tab = mainWindow.find("#"+tabId);  
    var isHome = selectMenu.hasClass("homepage");
    var tabContent = selectMenu.find(".menu_link").html();
	if(tab.length < 1){ //不存在添加tab、iframe
		   //添加tab标签
		     var tabHtml = "";
		     if(!isHome)  {
		    	 tabHtml = " <div class='tabtitle' id='"+tabId+"'><span>"+tabContent+"</span><span class='closetab'>X</span></div>";
		     }   
		     else   {
		    	 tabHtml = " <div class='tabtitle' id='"+tabId+"'><span>"+tabContent+"</span></div>"; 
		     }   
		    mainWindow.find("div.tabs").append(tabHtml);  
		 	$.ajax({
			   url:URL.FRAMEWORK+"/menu/forword?id="+menuId,
			   success:function(response){ 
				   var responseUrl =  response.data;
				   var f = "<div  calss='iframe_container' id='"+iframeId+"'><iframe  class='tab_iframe' src='"+responseUrl+"'></iframe></div>";
				  mainWindow.find("div.iframes").append(f); 
			   }
			})    
	}
	//隐藏其他选中的tab 、iframe
	var oldSelectTab = mainWindow.find("div.tabselect");
	    oldSelectTab.css("background-color","").toggleClass("tabselect");
	var oldTabId = oldSelectTab.attr("id"); 
	    mainWindow.find("#"+iframePrefix+oldTabId).hide();
	
	//显示当前选中的tab 、iframe
	newSelectTab = mainWindow.find("#"+tabId);
	newSelectTab.toggleClass("tabselect");
	mainWindow.find("#"+iframeId).show(); 
	
	return newSelectTab;
}
 
function changeColor(that,newSelectTab){  
	var top = $("#top_iframe",window.top.document).contents();  
	var color = top.find(".active-skin").css('background-color'); 
	if(!color) return;
	rgb = color.replace(/(rgb\()|(\))/g,'').split(","); 
	rgb[0] = Number(rgb[0]);
	rgb[1] = Number(rgb[1]);
	rgb[2] = Number(rgb[2]);  
	var newRgb = []; 
	newRgb.push(rgb[0] +(255-rgb[0])/1.2);
	newRgb.push(rgb[1] +(255-rgb[1])/1.2);
	newRgb.push(rgb[2] +(255-rgb[2])/1.2); 
	var activeColor  = "rgb("+newRgb.join(",")+")"; 
	that.css("background-color",activeColor); 
	$(".topbg").css('background-color',color);
	if(newSelectTab){
		newSelectTab.css("background-color",activeColor);  
	}
}

 
